import { ref } from 'vue'
import { defineStore } from 'pinia'
import { getUsersApi,getWeatherApi,getWeatherApiAll } from '../api/respont'
export const useCounterStore = defineStore('counter', () => {
  // 获取到code

 const localcode = ref('')
 const weatherTody= ref('')
 const weatherTodylocal= ref('')
 const echartDays = ref([])
 const echartNight = ref([])
 const items = ref([])
 const option=ref()
 const codeItem=ref(JSON.parse(localStorage.getItem('weather')) || [])
 const onlycodeItem =ref( Array.from(new Set(codeItem.value)))
 const citylist1 = ref([])
//  获取ip所在地城市acode
const  adcode= async ()=>{
  const res = await  getUsersApi()
  // console.log(res,'4444444')
  localcode.value=res.adcode
}

// 在localstorg存储需要实时查看的城市
const handleSave=(id)=>{
  codeItem.value.push(id)
  localStorage.setItem('weather',JSON.stringify(codeItem.value))
}

// 实现删除功能
function delList(data) {
  const index = codeItem.value.findIndex(item=>item === data)
  codeItem.value.splice(index,1)

  localStorage.removeItem('weather')
  localStorage.setItem('weather', JSON.stringify(codeItem.value))
  
  // 自动刷新页面
  history.go(0)
}




// 获取已保存城市的天气情况
  const weatherallcode = ()=>{
  const res = onlycodeItem.value.map((item)=>getWeatherApi(item))
  Promise.all(res).then((res1)=>{
    // console.log(res1,'333')
    const obj = res1.map((item)=>{
      return {
        name:item.lives[0].city,
        value:item.lives[0].temperature,
        code:item.lives[0].adcode
      }
    })
    citylist1.value.push(obj)
    // console.log(citylist1.value[0],'444')
  })
}

// 获取未来四天的天气
const weatherall =  async(id)=>{
  const res =  await getWeatherApiAll(id)
  // console.log(res.forecasts[0].casts,'000')
  items.value=res.forecasts[0].casts
  echartDays.value = res.forecasts[0].casts.map(item=>{
    return item.daytemp
  })
  echartNight.value = res.forecasts[0].casts.map(item=>{
    return item.nighttemp
  })
//  console.log(echartDays.value)
}

// 获取搜索城市当天的天气
const  weathertoday =async(id)=>{
  const res = await getWeatherApi(id)
  weatherTody.value=res.lives[0]
  // console.log(res.lives[0],'77777')
}


// 获取ip所在地城市当天的天气
const  localweathertoday =async()=>{
  const res = await getWeatherApi(localcode.value)
  weatherTodylocal.value=res.lives[0]
  // console.log(res,'99999')
}

// 设置图表option配置项
const optionget =()=>{
  option.value={
    xAxis: {
      show:false,
      type: 'category',
    },
    yAxis: {
      show:false,
      type: 'value'
    },
    grid:{
      left:0,
      top:30,
      right:0,
      bottom:0,
    },
    series: [
      {
        data:echartDays,
        type: 'line',
        smooth: true,
       
        label:{
          show:true,
          position:"top",
          fontStyle: "normal",
          color:"#fff",
          formatter:(params)=>{
            return '白'+params.value+'℃'
          }
        }
      },
      {
        data: echartNight,
        type: 'line',
        label:{
          show:true,
          position:"bottom",
          fontStyle: "normal",
          color:"#fff",
          formatter:(params)=>{
            return '晚'+params.value+'℃'
          }
        }
      },
    ]
  }
}
  return { adcode,weatherall,weathertoday,optionget,localweathertoday,handleSave,weatherallcode,delList,
    localcode,weatherTody,weatherTodylocal,echartDays,echartNight,items,option,citylist1} 
})
